<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<div class="property-table flex flex-col h-full gap-y-3">
    <div class="flex justify-between items-center">
        <div class="font-bold">Required field</div>
        @if (!isDisabled) {
            <div>
                <button mat-icon-button class="primary-icon-button" type="button" (click)="newPropertyClicked()">
                    <i class="fa fa-plus"></i>
                </button>
            </div>
        }
    </div>
    <div class="listing-table flex-1 relative">
        <div class="absolute inset-0 overflow-y-auto overflow-x-hidden">
            <table mat-table #propertyTable [dataSource]="dataSource">
                <!-- Property Column -->
                <ng-container matColumnDef="property">
                    <th mat-header-cell *matHeaderCellDef>Property</th>
                    <td mat-cell *matCellDef="let item" [class.font-bold]="item.descriptor.required">
                        <div class="flex justify-between items-center">
                            <div
                                class="whitespace-nowrap overflow-hidden text-ellipsis"
                                [title]="item.descriptor.displayName">
                                {{ item.descriptor.displayName }}
                            </div>
                            <i
                                class="fa fa-info-circle primary-color"
                                nifiTooltip
                                [tooltipComponentType]="PropertyTip"
                                [tooltipInputData]="getPropertyTipData(item)"
                                [delayClose]="false"></i>
                        </div>
                    </td>
                </ng-container>

                <!-- Value Column -->
                <ng-container matColumnDef="value">
                    <th mat-header-cell *matHeaderCellDef>Value</th>
                    <td mat-cell *matCellDef="let item">
                        <div
                            [id]="formatId(item)"
                            class="pointer min-h-[21px]"
                            cdkOverlayOrigin
                            #trigger="cdkOverlayOrigin"
                            (click)="openEditor(trigger, item, $event)">
                            @if (isNull(item.value)) {
                                <div class="unset neutral-color">No value set</div>
                            } @else {
                                <ng-container
                                    *ngTemplateOutlet="
                                        isSensitiveProperty(item.descriptor) ? sensitive : nonSensitive;
                                        context: { $implicit: resolvePropertyValue(item) }
                                    "></ng-container>
                                <ng-template #sensitive>
                                    <div class="sensitive neutral-color">Sensitive value set</div>
                                </ng-template>
                                <ng-template #nonSensitive let-resolvedValue>
                                    <ng-container
                                        *ngTemplateOutlet="
                                            isEmptyString(resolvedValue) ? blank : nonBlank;
                                            context: { $implicit: resolvedValue }
                                        "></ng-container>
                                </ng-template>
                                <ng-template #blank>
                                    <div class="empty neutral-color">Empty string set</div>
                                </ng-template>
                                <ng-template #nonBlank let-resolvedValue>
                                    <div class="flex justify-between items-center">
                                        <div
                                            class="whitespace-nowrap overflow-hidden text-ellipsis"
                                            nifiTooltip
                                            [tooltipComponentType]="PropertyValueTip"
                                            [tooltipInputData]="getPropertyValueTipData(item)"
                                            [position]="tooltipPosition"
                                            [delayClose]="false">
                                            {{ resolvedValue }}
                                        </div>
                                        @if (hasExtraWhitespace(resolvedValue)) {
                                            <div
                                                class="fa fa-info-circle primary-color"
                                                nifiTooltip
                                                [tooltipComponentType]="TextTip"
                                                tooltipInputData="The specified value contains leading and/or trailing whitespace character(s). This could produce unexpected results if it was not intentional."></div>
                                        }
                                    </div>
                                </ng-template>
                            }
                        </div>
                    </td>
                </ng-container>

                <!-- Actions Column -->
                <ng-container matColumnDef="actions">
                    <th mat-header-cell *matHeaderCellDef></th>
                    <td mat-cell *matCellDef="let item">
                        <div class="flex items-center justify-end gap-x-2">
                            @if (
                                canGoToParameter(item) ||
                                canGoToService(item) ||
                                (!isDisabled
                                    ? item.descriptor.identifiesControllerService ||
                                      canConvertToParameter(item) ||
                                      item.type == 'userDefined'
                                    : false)
                            ) {
                                <button
                                    mat-icon-button
                                    type="button"
                                    [matMenuTriggerFor]="actionMenu"
                                    class="h-16 w-16 flex items-center justify-center icon global-menu">
                                    <i class="fa fa-ellipsis-v"></i>
                                </button>
                            }
                            <mat-menu #actionMenu="matMenu" xPosition="before">
                                @if (canGoToParameter(item)) {
                                    <button mat-menu-item (click)="goToParameterClicked(item)">
                                        <i class="fa fa-long-arrow-right primary-color mr-2"></i>
                                        Go to Parameter
                                    </button>
                                }
                                @if (canGoToService(item)) {
                                    <button mat-menu-item (click)="goToServiceClicked(item)">
                                        <i class="fa fa-long-arrow-right primary-color mr-2"></i>
                                        Go to Service
                                    </button>
                                }
                                @if (!isDisabled) {
                                    @if (item.descriptor.identifiesControllerService) {
                                        <button mat-menu-item (click)="createNewControllerService(item)">
                                            <i class="fa fa-plus primary-color mr-2"></i>
                                            Create new service
                                        </button>
                                    }
                                    @if (canConvertToParameter(item)) {
                                        <button mat-menu-item (click)="convertToParameterClicked(item)">
                                            <i class="fa fa-level-up primary-color mr-2"></i>
                                            Convert to Parameter
                                        </button>
                                    }
                                    @if (item.type == 'userDefined') {
                                        <button mat-menu-item (click)="deleteProperty(item)">
                                            <i class="fa fa-trash primary-color mr-2"></i>
                                            Delete
                                        </button>
                                    }
                                }
                            </mat-menu>
                        </div>
                    </td>
                </ng-container>

                <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
                <tr
                    mat-row
                    *matRowDef="let row; let even = even; columns: displayedColumns"
                    (click)="selectProperty(row)"
                    [class.selected]="isSelected(row)"
                    [class.even]="even"></tr>
            </table>
            <ng-template
                cdkConnectedOverlay
                [cdkConnectedOverlayOrigin]="editorTrigger"
                [cdkConnectedOverlayPositions]="editorPositions"
                [cdkConnectedOverlayPush]="true"
                [cdkConnectedOverlayHasBackdrop]="true"
                [cdkConnectedOverlayBackdropClass]="'cdk-overlay-transparent-backdrop'"
                [cdkConnectedOverlayOpen]="editorOpen"
                (detach)="closeEditor()">
                @if (hasAllowableValues(editorItem)) {
                    <combo-editor
                        [item]="editorItem"
                        [parameterConfig]="editorParameterConfig"
                        [width]="editorWidth"
                        [readonly]="isDisabled"
                        (ok)="savePropertyValue(editorItem, $event)"
                        (exit)="closeEditor()"></combo-editor>
                } @else {
                    <nf-editor
                        [parameterConfig]="editorParameterConfig"
                        [item]="editorItem"
                        [width]="editorWidth"
                        [readonly]="isDisabled"
                        (ok)="savePropertyValue(editorItem, $event)"
                        (exit)="closeEditor()"></nf-editor>
                }
            </ng-template>
        </div>
    </div>
</div>
